using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Json;
using System.Threading.Tasks;
using BaseData.CommonLib.Constants;
using BaseData.CommonLib.Models;
using Newtonsoft.Json;
using Swimj.CommonLib.Base;
using Swimj.Core.Extensions;

namespace BaseData.RazorLib.Services
{
    public class SupplierService : ISupplierService
    {

        private readonly IHttpClientFactory _httpClientFactory;

        public SupplierService(IHttpClientFactory httpClientFactory)
        {
            this._httpClientFactory = httpClientFactory;
        }

        public async Task<Result<SupplierInfoResponseDto>?> CreateSupplier(SupplierEditRequestDto model)
        {
            try
            {
                var httpClient = _httpClientFactory.CreateClient("basedataApi");
                var response =
                    await httpClient.PostAsJsonAsync($"/{ApiRoutes.Supplier.CreateSupplier}", model);
                if (response.IsSuccessStatusCode)
                {
                    var resultJson = await response.Content.ReadAsStringAsync();

                    if (!string.IsNullOrEmpty(resultJson))
                    {
                        return JsonConvert.DeserializeObject<Result<SupplierInfoResponseDto>>(resultJson);
                    }
                }

                return new Result<SupplierInfoResponseDto>();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

        public async Task<Result<SupplierInfoResponseDto>?> GetSupplier(int key)
        {
            try
            {
                var httpClient = _httpClientFactory.CreateClient("basedataApi");
                var response = await httpClient.GetAsync($"/{ApiRoutes.Supplier.GetSupplier.Replace("{id}", key.ToString())}");
                if (response.IsSuccessStatusCode)
                {
                    var resultJson = await response.Content.ReadAsStringAsync();

                    if (!string.IsNullOrEmpty(resultJson))
                    {
                        return JsonConvert.DeserializeObject<Result<SupplierInfoResponseDto>>(resultJson);
                    }
                }

                return null;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

        public Task<Result<IList<SupplierInfoResponseDto>>?> QuerySuppliers(SupplierSearchRequestDto searchRequestDto)
        {
            throw new NotImplementedException();
        }

        public async Task<PaginatedResult<SupplierInfoResponseDto>?> QuerySuppliersByPage(SupplierSearchRequestDto searchRequestDto)
        {
            try
            {
                var httpClient = _httpClientFactory.CreateClient("basedataApi");
                var response = await httpClient.GetAsync(
                    searchRequestDto.ToQueryString(ApiRoutes.Supplier.QuerySuppliers));
                if (response.IsSuccessStatusCode)
                {
                    var resultJson = await response.Content.ReadAsStringAsync();

                    if (!string.IsNullOrEmpty(resultJson))
                    {
                        return JsonConvert.DeserializeObject<PaginatedResult<SupplierInfoResponseDto>>(resultJson);
                    }
                }

                return null;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

        public async Task<Result<SupplierInfoResponseDto>?> UpdateSupplier(int id, SupplierEditRequestDto editRequestDto)
        {
            try
            {
                var httpClient = _httpClientFactory.CreateClient("basedataApi");
                var response = await httpClient.PutAsJsonAsync($"/{ApiRoutes.Supplier.UpdateSupplier.Replace("{id}", id.ToString())}", editRequestDto);
                if (response.IsSuccessStatusCode)
                {
                    var resultJson = await response.Content.ReadAsStringAsync();

                    if (!string.IsNullOrEmpty(resultJson))
                    {
                        return JsonConvert.DeserializeObject<Result<SupplierInfoResponseDto>>(resultJson);
                    }
                }

                return new Result<SupplierInfoResponseDto>();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
    }
}